package comp.display 
{
	import comp.event.CompEvent;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormat;
		
	/**
	 * ...
	 * @author Qi Donghui
	 */
	public class Window extends DisplayComp 
	{	
		protected var _title:String;
		
		protected var _headBg:MovieClip;
		
		protected var _cont:Sprite;
		
		protected var _closeBtn:DisplayObject;
		
		private var _source:DisplayObject;
		
		private var _userDragged:Boolean = false;
		
		public function Window(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, title:String="Window") 
		{
			_title = title;
			_cont = new Sprite();
			addChild(_cont);	
			super(parent, xpos, ypos);
		}
		
		/**
		 * Initializes the component.
		 */
		override protected function init():void
		{
			super.init();
			setSize(500, 100);
		}
		
		/**
		 * Creates and adds the child display objects of this component.
		 */
		override protected function addChildren():void
		{		
			_headBg = new SKwindowTopBg();
			_headBg.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			_cont.addChild(_headBg);		
			_headBg.width = 500;
			
			var tf:TextFormat = new TextFormat();
			tf.size = 14;
			tf.color = 0xe5e5e5;
			tf.bold = true;
			var titleTxt:Label = new Label(_cont, 7, 5, _title, tf);
			
			_closeBtn = new SKpanelCloseBtn();
			_closeBtn.x = _headBg.width - 23;
			_closeBtn.y = 10;
			_closeBtn.addEventListener(MouseEvent.CLICK, onCloseBtnClick);
			_cont.addChild(_closeBtn);
		}
		
		///////////////////////////////////
		// public methods
		///////////////////////////////////
		
		/**
		 * Draws the visual ui of the component.
		 */
		override public function draw():void
		{
			super.draw();			
			_cont.graphics.clear();
			_cont.graphics.lineStyle(1, 0x898989, 1);
			_cont.graphics.beginFill(0xffffff);
			_cont.graphics.drawRect(-0.5, -0.5, _width + 1, _height + 1);
			_cont.graphics.endFill();
			
			_headBg.width = _width;
			_closeBtn.x = _headBg.width - 23;	
			
			onStageResize();
			stage.addEventListener(Event.RESIZE, onStageResize);
		}
		
		/**
		 * Moves the component to the specified position.
		 * @param xpos the x position to move the component
		 * @param ypos the y position to move the component
		 */
		override public function move(xpos:Number, ypos:Number):void
		{
			_cont.x = Math.round(xpos);
			_cont.y = Math.round(ypos);
		}		
		
		/**
		 * 使得窗口剧中显示
		 */
		public function centerWindow():void
		{
			move(stage.stageWidth * 0.5 - _width * 0.5, stage.stageHeight * 0.5 - _height * 0.5);
		}
		
		///////////////////////////////////
		// event handlers
		///////////////////////////////////	
		/**
		 * Internal mouseDown handler. Starts a drag.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseDown(event:MouseEvent):void
		{
			_userDragged = true;
			var rec:Rectangle = new Rectangle(0, 0, stage.stageWidth - _width, stage.stageHeight - _height);		
			_cont.startDrag(false, rec);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			parent.addChild(this); // move to top			
		}
		
		/**
		 * Internal mouseUp handler. Stops the drag.
		 * @param event The MouseEvent passed by the system.
		 */
		protected function onMouseUp(event:MouseEvent):void
		{
			this.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onCloseBtnClick(e:MouseEvent):void 
		{
			dispatchEvent(new CompEvent(CompEvent.CLOSE));
			stage.removeEventListener(Event.RESIZE, onStageResize);
			parent.removeChild(this);		
		}
		
		private function onStageResize(evt:Event = null):void
		{
			this.graphics.clear();	
			this.graphics.beginFill(0x000000, 0.2);
			this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			this.graphics.endFill();
			
			if(!_userDragged) centerWindow();
		}
		
		///////////////////////////////////
		// set & get
		///////////////////////////////////			
		public function get source():DisplayObject { return _source; }
		public function set source(value:DisplayObject):void 
		{
			_userDragged = false;
			if (_source != null) _cont.removeChild(_source);
			_source = value;
			if (_source == null) return;
			_source.y = _headBg.height;
			_cont.addChild(_source);
		}
		
	}

}